package cn.iocoder.yudao.module.sys.service.instrumentoperationlog;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.sys.controller.admin.instrumentoperationlog.vo.*;
import cn.iocoder.yudao.module.sys.dal.dataobject.instrumentoperationlog.InstrumentOperationLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.sys.dal.mysql.instrumentoperationlog.InstrumentOperationLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.sys.enums.ErrorCodeConstants.*;

/**
 * 仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯） Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InstrumentOperationLogServiceImpl implements InstrumentOperationLogService {

    @Resource
    private InstrumentOperationLogMapper instrumentOperationLogMapper;

    @Override
    public Long createInstrumentOperationLog(InstrumentOperationLogSaveReqVO createReqVO) {
        // 插入
        InstrumentOperationLogDO instrumentOperationLog = BeanUtils.toBean(createReqVO, InstrumentOperationLogDO.class);
        instrumentOperationLogMapper.insert(instrumentOperationLog);

        // 返回
        return instrumentOperationLog.getId();
    }

    @Override
    public void updateInstrumentOperationLog(InstrumentOperationLogSaveReqVO updateReqVO) {
        // 校验存在
        validateInstrumentOperationLogExists(updateReqVO.getId());
        // 更新
        InstrumentOperationLogDO updateObj = BeanUtils.toBean(updateReqVO, InstrumentOperationLogDO.class);
        instrumentOperationLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteInstrumentOperationLog(Long id) {
        // 校验存在
        validateInstrumentOperationLogExists(id);
        // 删除
        instrumentOperationLogMapper.deleteById(id);
    }

    @Override
        public void deleteInstrumentOperationLogListByIds(List<Long> ids) {
        // 删除
        instrumentOperationLogMapper.deleteByIds(ids);
        }


    private void validateInstrumentOperationLogExists(Long id) {
        if (instrumentOperationLogMapper.selectById(id) == null) {
            throw exception(INSTRUMENT_OPERATION_LOG_NOT_EXISTS);
        }
    }

    @Override
    public InstrumentOperationLogDO getInstrumentOperationLog(Long id) {
        return instrumentOperationLogMapper.selectById(id);
    }

    @Override
    public PageResult<InstrumentOperationLogDO> getInstrumentOperationLogPage(InstrumentOperationLogPageReqVO pageReqVO) {
        return instrumentOperationLogMapper.selectPage(pageReqVO);
    }

}